package sample.test.cc.cool.gui.container;

import java.awt.GridBagLayout;
import java.awt.Rectangle;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

import sample.test.cc.cool.gui.controllers.AdmController;
import sample.test.cc.cool.gui.controllers.AgentContainerController;
import sample.test.cc.cool.gui.controllers.ClientController;
import sample.test.cc.cool.gui.util.Containable;
import sample.test.cc.cool.gui.util.IntegerValidator;
import BESA.SystemLevel.AdmBESA;

public class GuiContainer extends JFrame implements Containable{

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null; // @jve:decl-index=0:visual-constraint="4,10"
	private JLabel jLabel = null;
	private JLabel ipLabel = null;
	private JLabel jLabel1 = null;
	private JLabel multicastLabel = null;
	private JTabbedPane jTabbedPane = null;
	private JPanel gccPanel = null;
	private JPanel greenPagesPanel = null;
	private JTabbedPane AgentMainPane = null;
	private JPanel agentCreatePanel = null;
	private JPanel lifeAgentPanel = null;
	private JLabel jLabel2 = null;
	private JLabel jLabel3 = null;
	private JTextField agentNameField = null;
	private JTextField valueField = null;
	private JButton cancelButton = null;
	private JButton createButton = null;
	private JScrollPane jScrollPane = null;
	private JTable lifeAgentsTable = null;
	private JLabel activeTransactionLabel = null;
	private JScrollPane jScrollPane1 = null;
	private JTable gccAgentsTable = null;
	private JLabel jLabel4 = null;
	private JPanel greenPagesAgents = null;
	private JLabel jLabel5 = null;
	private JComboBox liveAgentsGreenPagesComboBox = null;
	private JLabel jLabel6 = null;
	private JComboBox groupsAgentsGreenPagesComboBox = null;
	private JButton associateButton = null;
	private JLabel jLabel7 = null;
	private JLabel portMCLabel = null;
	private JScrollPane jScrollPane2 = null;
	private JList transactionList = null;
	private JLabel status = null;
	private JLabel stateLabel = null;
	/**
	 * This is the default constructor
	 */
	public GuiContainer() {
		super();
		initialize();
		if(AdmController.existContainner){
			System.out.println("OK");
			updateContainnerMetaData();
		}
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(624, 409);
		this.setResizable(false);
		this.setContentPane(getJContentPane());
		this.setTitle("eBESA - ");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			portMCLabel = new JLabel();
			portMCLabel.setBounds(new Rectangle(510, 20, 61, 21));
			portMCLabel.setHorizontalAlignment(SwingConstants.CENTER);
			portMCLabel.setText("-");
			jLabel7 = new JLabel();
			jLabel7.setBounds(new Rectangle(410, 20, 91, 21));
			jLabel7.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabel7.setText("Multicast port:");
			multicastLabel = new JLabel();
			multicastLabel.setText("-");
			multicastLabel.setBounds(new Rectangle(290, 20, 111, 21));
			multicastLabel.setHorizontalAlignment(SwingConstants.CENTER);
			multicastLabel.setText("JLabel");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(170, 20, 111, 21));
			jLabel1.setHorizontalTextPosition(SwingConstants.RIGHT);
			jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabel1.setText("Multicast Address:");
			ipLabel = new JLabel();
			ipLabel.setBounds(new Rectangle(40, 20, 106, 21));
			ipLabel.setHorizontalAlignment(SwingConstants.CENTER);
			ipLabel.setHorizontalTextPosition(SwingConstants.RIGHT);
			ipLabel.setText("-");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(10, 20, 24, 21));
			jLabel.setHorizontalTextPosition(SwingConstants.RIGHT);
			jLabel.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabel.setText("IP:");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabel, null);
			jContentPane.add(ipLabel, null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(multicastLabel, null);
			jContentPane.add(getJTabbedPane(), null);
			jContentPane.add(jLabel7, null);
			jContentPane.add(portMCLabel, null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTabbedPane
	 * 
	 * @return javax.swing.JTabbedPane
	 */
	private JTabbedPane getJTabbedPane() {
		if (jTabbedPane == null) {
			jTabbedPane = new JTabbedPane();
			jTabbedPane.addTab("Agents", null, getAgentMainPane(), null);
			jTabbedPane.setBounds(new Rectangle(0, 60, 616, 321));
			jTabbedPane.addTab("GCC - Monitor", null, getGccPanel(), null);
			jTabbedPane.addTab("Green Pages", null, getGreenPagesPanel(), null);
		}
		return jTabbedPane;
	}

	/**
	 * This method initializes gccPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getGccPanel() {
		if (gccPanel == null) {
			stateLabel = new JLabel();
			stateLabel.setBounds(new Rectangle(470, 10, 71, 21));
			stateLabel.setHorizontalAlignment(SwingConstants.CENTER);
			stateLabel.setText("-");
			status = new JLabel();
			status.setBounds(new Rectangle(420, 10, 41, 21));
			status.setHorizontalAlignment(SwingConstants.RIGHT);
			status.setText("State:");
			jLabel4 = new JLabel();
			jLabel4.setBounds(new Rectangle(140, 10, 141, 21));
			jLabel4.setText("Agents per trasaction");
			activeTransactionLabel = new JLabel();
			activeTransactionLabel.setBounds(new Rectangle(10, 10, 111, 21));
			activeTransactionLabel.setText("Active Trasanction");
			gccPanel = new JPanel();
			gccPanel.setLayout(null);
			gccPanel.add(activeTransactionLabel, null);
			gccPanel.add(getJScrollPane1(), null);
			gccPanel.add(jLabel4, null);
			gccPanel.add(getJScrollPane2(), null);
			gccPanel.add(status, null);
			gccPanel.add(stateLabel, null);
		}
		return gccPanel;
	}

	/**
	 * This method initializes greenPagesPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getGreenPagesPanel() {
		if (greenPagesPanel == null) {
			greenPagesPanel = new JPanel();
			greenPagesPanel.setLayout(new GridBagLayout());
		}
		return greenPagesPanel;
	}

	/**
	 * This method initializes AgentMainPane
	 * 
	 * @return javax.swing.JTabbedPane
	 */
	private JTabbedPane getAgentMainPane() {
		if (AgentMainPane == null) {
			AgentMainPane = new JTabbedPane();
			AgentMainPane.addTab("Create", null, getAgentCreatePanel(), null);
			AgentMainPane.addTab("Green Pages Agents", null,
					getGreenPagesAgents(), null);
			AgentMainPane
					.addTab("Live Agents", null, getLifeAgentPanel(), null);

		}
		return AgentMainPane;
	}

	/**
	 * This method initializes agentCreatePanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getAgentCreatePanel() {
		if (agentCreatePanel == null) {
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(50, 120, 111, 21));
			jLabel3.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabel3.setText("Value:");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(50, 70, 111, 21));
			jLabel2.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabel2.setText("Agent name:");
			agentCreatePanel = new JPanel();
			agentCreatePanel.setLayout(null);
			agentCreatePanel.add(jLabel2, null);
			agentCreatePanel.add(jLabel3, null);
			agentCreatePanel.add(getAgentNameField(), null);
			agentCreatePanel.add(getValueField(), null);
			agentCreatePanel.add(getCancelButton(), null);
			agentCreatePanel.add(getCreateButton(), null);
		}
		return agentCreatePanel;
	}

	/**
	 * This method initializes lifeAgentPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getLifeAgentPanel() {
		if (lifeAgentPanel == null) {
			lifeAgentPanel = new JPanel();
			lifeAgentPanel.setLayout(null);
			lifeAgentPanel.add(getJScrollPane(), null);
		}
		return lifeAgentPanel;
	}

	/**
	 * This method initializes agentNameField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getAgentNameField() {
		if (agentNameField == null) {
			agentNameField = new JTextField();
			agentNameField.setBounds(new Rectangle(180, 70, 201, 21));
		}
		return agentNameField;
	}

	/**
	 * This method initializes valueField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getValueField() {
		if (valueField == null) {
			valueField = new JTextField();
			valueField.setInputVerifier(new IntegerValidator());
			valueField.setBounds(new Rectangle(180, 120, 101, 21));
		}
		return valueField;
	}

	/**
	 * This method initializes cancelButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setBounds(new Rectangle(410, 200, 91, 21));
			cancelButton.setText("Cancel");
			cancelButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					agentNameField.setText("");
					valueField.setText("");
				}
			});
		}
		return cancelButton;
	}

	/**
	 * This method initializes createButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCreateButton() {
		if (createButton == null) {
			createButton = new JButton();
			createButton.setBounds(new Rectangle(310, 200, 81, 21));
			createButton.setText("Create");
			createButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {

					if (checkAgentCreationFields()) {
						AgentContainerController.getInstance().createAgent(
								agentNameField.getText(),
								Integer.parseInt(valueField.getText()));
						
						showOkDialog("Agent created successful", "Creation");
						
					}
				}
			});
		}
		return createButton;
	}

	protected void showOkDialog(String string, String string2) {
		JOptionPane.showMessageDialog((JFrame) this, string, "eBESA - "
				+ string2, DISPOSE_ON_CLOSE);

	}

	private boolean checkAgentCreationFields() {
		String agN = agentNameField.getText();
		if (!agN.isEmpty()
				&& !valueField.getText().isEmpty()) {
			if(agN.contains("_COPY_")){
				JOptionPane.showMessageDialog(this, "Invalid agent name",
						"Error", JOptionPane.ERROR_MESSAGE);
				return false;
			}else{
				if(ClientController.getInstance().getAllAgentsAlias().contains(agN)){
					JOptionPane.showMessageDialog(this, "Agent name alredy in use",
							"Error", JOptionPane.ERROR_MESSAGE);
					return false;
				}else{
					try {
						Integer.parseInt(valueField.getText());
					} catch (NumberFormatException e2) {
						JOptionPane.showMessageDialog(this, "Value must be a number",
								"Error", JOptionPane.ERROR_MESSAGE);
						return false;
					}
				}
			}
			return true;
		} else {

			return false;
		}
	}

	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(10, 20, 511, 211));
			jScrollPane.setViewportView(getLifeAgentsTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes lifeAgentsTable
	 * 
	 * @return javax.swing.JTable
	 */
	private JTable getLifeAgentsTable() {
		if (lifeAgentsTable == null) {
			lifeAgentsTable = new JTable();
			DefaultTableModel df = new DefaultTableModel();
			df.addColumn("Agent name");
			df.addColumn("Value");
			df.addColumn("Society");
			df.addColumn("Gruop(s)");
			lifeAgentsTable.setModel(df);
		}
		return lifeAgentsTable;
	}

	/**
	 * This method initializes jScrollPane1
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane1() {
		if (jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.setBounds(new Rectangle(140, 40, 466, 221));
			jScrollPane1.setViewportView(getGccAgentsTable());
		}
		return jScrollPane1;
	}

	/**
	 * This method initializes gccAgentsTable
	 * 
	 * @return javax.swing.JTable
	 */
	public JTable getGccAgentsTable() {
		if (gccAgentsTable == null) {
			gccAgentsTable = new JTable();
			DefaultTableModel df = new DefaultTableModel();
			df.addColumn("Agent name");
			df.addColumn("Agent ID");
			df.addColumn("Operation");
			df.addColumn("Vote");
			gccAgentsTable.setModel(df);
			
		}
		return gccAgentsTable;
	}
	

	/**
	 * This method initializes greenPagesAgents
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getGreenPagesAgents() {
		if (greenPagesAgents == null) {
			jLabel6 = new JLabel();
			jLabel6.setBounds(new Rectangle(120, 110, 81, 21));
			jLabel6.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabel6.setText("Group:");
			jLabel5 = new JLabel();
			jLabel5.setBounds(new Rectangle(120, 70, 81, 21));
			jLabel5.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabel5.setText("Agent:");
			greenPagesAgents = new JPanel();
			greenPagesAgents.setLayout(null);
			greenPagesAgents.add(jLabel5, null);
			greenPagesAgents.add(getLiveAgentsGreenPagesComboBox(), null);
			greenPagesAgents.add(jLabel6, null);
			greenPagesAgents.add(getGroupsAgentsGreenPagesComboBox(), null);
			greenPagesAgents.add(getAssociateButton(), null);
		}
		return greenPagesAgents;
	}

	/**
	 * This method initializes liveAgentsGreenPagesComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getLiveAgentsGreenPagesComboBox() {
		if (liveAgentsGreenPagesComboBox == null) {
			liveAgentsGreenPagesComboBox = new JComboBox();
			liveAgentsGreenPagesComboBox.setBounds(new Rectangle(230, 70, 181, 21));
		}
		return liveAgentsGreenPagesComboBox;
	}

	/**
	 * This method initializes groupsAgentsGreenPagesComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getGroupsAgentsGreenPagesComboBox() {
		if (groupsAgentsGreenPagesComboBox == null) {
			groupsAgentsGreenPagesComboBox = new JComboBox();
			groupsAgentsGreenPagesComboBox.setBounds(new Rectangle(230, 110, 181, 21));
		}
		return groupsAgentsGreenPagesComboBox;
	}

	/**
	 * This method initializes associateButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getAssociateButton() {
		if (associateButton == null) {
			associateButton = new JButton();
			associateButton.setBounds(new Rectangle(380, 200, 101, 21));
			associateButton.setText("Associete");
		}
		return associateButton;
	}
	
	public void updateContainnerMetaData(){
		multicastLabel.setText(AdmBESA.getInstance().getMulticastInetAddr().toString().substring(1));
		ipLabel.setText(AdmBESA.getInstance().getIP());
		portMCLabel.setText(AdmBESA.getInstance().getMulticastPort()+"");
		this.setTitle("eBESA - "+AdmBESA.getInstance().getAlias()+ " - Agent Containner");
	}

	/**
	 * This method initializes jScrollPane2	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane2() {
		if (jScrollPane2 == null) {
			jScrollPane2 = new JScrollPane();
			jScrollPane2.setBounds(new Rectangle(15, 44, 106, 217));
			jScrollPane2.setViewportView(getTransactionList());
		}
		return jScrollPane2;
	}

	/**
	 * This method initializes transactionList	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getTransactionList() {
		if (transactionList == null) {
			transactionList = new JList();
			transactionList.setModel(new DefaultListModel());
			transactionList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
						public void valueChanged(javax.swing.event.ListSelectionEvent e) {
							long id = (Long)transactionList.getSelectedValue();
							AgentContainerController.getInstance().setSelectedTransactionGCC(id);
							AgentContainerController.getInstance().updateTransactionData(id);
						}
					});
		}
		return transactionList;
	}
	
	public void updateTransactionList(long transaction){
		DefaultListModel model = (DefaultListModel) this.getTransactionList().getModel();
		model.addElement(transaction);
		this.getTransactionList().repaint();
	}
	
	/**
	 * 
	 * @param data {Agent name, Value, Society, Groups}
	 */
	public void addAgentToAgentsTable(String[] data){
		DefaultTableModel model = (DefaultTableModel) getLifeAgentsTable().getModel();
		model.addRow((Object[])data);
		getLifeAgentsTable().setModel(model);
	}
	
	
	public void updateStateLabel(String newState){
		this.stateLabel.setText(newState);
	}
	

} // @jve:decl-index=0:visual-constraint="10,10"
